package io.renren.modules.neo4j.service.impl;

import io.renren.modules.neo4j.entity.node.course;
import io.renren.modules.neo4j.entity.node.knowledge;
import io.renren.modules.neo4j.respository.KnowledgeRespository;
import io.renren.modules.neo4j.service.KnowledgeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

@Service
public class KnowledgeServiceImpl implements KnowledgeService {

    @Autowired
    KnowledgeRespository knowledgeRespository;

    @Override
    public knowledge getKnowledgeById(Long id) {
        Optional<knowledge> k=knowledgeRespository.findById(id,1);
        knowledge k1=new knowledge();
        k1.id=k.get().id;
        k1.setName(k.get().getName());
       if(k.get().getDownKnowledge()!=null) k1.setDownKnowledge(this.setKnowlegeList(k.get().getDownKnowledge()));
       if(k.get().getDownOutknowledge()!=null) k1.setDownOutknowledge(this.setKnowlegeList(k.get().getDownOutknowledge()));
       if(k.get().getUpKnowledge()!=null) k1.setUpKnowledge(this.setKnowlegeList(k.get().getUpKnowledge()));
        if(k.get().getUpOutknowledge()!=null) k1.setUpOutknowledge(this.setKnowlegeList(k.get().getUpOutknowledge()));
        if(k.get().getMaterial()!=null)k1.setMaterial(k.get().getMaterial());
        return k.get();
    }

    @Override
    public course getKnowledgeByCourseId(Long id) {
        Optional<knowledge> k= knowledgeRespository.findById(id,1);
        course c = new course();
        c.setKnowledge(setKnowlegeList(k.get().getDownKnowledge()));
        return c;
    }

    private List<knowledge> setKnowlegeList(List<knowledge> knowlegeList){
        List<knowledge> list=new ArrayList<>();

        for(knowledge k:knowlegeList){
            knowledge k1=new knowledge();
            k1.setName(k.getName());
            System.out.println(k.id);
            k1.id=k.id;
            list.add(k1);
        }
        return list;
    }
}
